Installing Flex (The fast lexical analyzer) on Ubuntu Linux

NOTE: You have to install m4 (general purpose macro processor) before installing Flex. Click here.

This short tutorial shows you how to install Flex (The fast lexical analyzer) on Ubuntu Linux Server. The process is very simple. For this tutorial, I'll be installing flex version 2.5.33. Linux kernel is 2.6.15-26-powerpc. I have used a Mac Mini (powerpc/ppc architecture). GCC version number is 4.0.3. Your configuration may be different from mine but the installation steps below should work fine for most configurations with little or no change at all (including other Linux distributions and even unixes e.g. MacOS X).

Flex (The fast lexical analyzer):

According to,

"Flex is a tool for generating scanners. A scanner, sometimes called a tokenizer, is a program which recognizes lexical patterns in text. The flex program reads user-specified input files, or its standard input if no file names are given, for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called rules. Flex generates a C source file named, "lex.yy.c", which defines the function yylex(). The file "lex.yy.c" can be compiled and linked to produce an executable. When the executable is run, it analyzes its input for occurrences of text matching the regular expressions for each rule. Whenever it finds a match, it executes the corresponding C code."

Before starting, please check to see if there is a latest version available to download. Visit to find out about the available versions. IMPORTANT: See "Configuring Ubuntu Linux After Installation" to install the development tools required to compile and install flex from source code.

Steps to download, compile, and install are as follows. Note: Replace 2.5.33 with your version number:

  • Downloading Flex (The fast lexical analyzer):

    Run the command below,
  • Extracting files from the downloaded package:

    tar -xvzf flex-2.5.33.tar.gz
    Now, enter the directory where the package is extracted.
    cd flex-2.5.33
  • Configuring flex before installation:

    If you haven't installed m4 yet then please do so. Click here to read about the installation instructions for m4. Run the commands below to include m4 in your PATH variable.


    NOTE: Replace '/usr/local/m4/bin' with the location of m4 binary. Now, configure the source code before installation.

    ./configure --prefix=/usr/local/flex

    Replace "/usr/local/flex" above with the directory path where you want to copy the files and folders. Note: check for any error message.

  • Compiling flex:


    Note: check for any error message.

  • Installing flex:

    As root (for privileges on destination directory), run the following.

    With sudo,
    sudo make install
    Without sudo,
    make install

    Note: check for any error messages.

That’s it. Flex has been successfully installed.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I recommend thoroughy testing on a production-like test system first before moving to production.

tags cloud icon tags cloud


popular searches icon popular searches

Comments (write a comment):

You should execute PATH=$PATH:/usr/local/flex/bin if you want use flex in every location (in terminal) :)

Of course /usr/local/flex is directory you chosed before "make" command Posted by: Artur on Mar 03, 2011

am getting the following errors:
*** [install-binPROGRAMS] Error 1
make[3]: Leaving directory `/home/nishath/m4-1.4.10/flex-2.5.33/m4-1.4.10/flex-2.5.33'
make[2]: *** [install-am] Error 2
make[2]: Leaving directory `/home/nishath/m4-1.4.10/flex-2.5.33/m4-1.4.10/flex-2.5.33'
make[1]: *** [install-recursive] Error 1
make[1]: Leaving directory `/home/nishath/m4-1.4.10/flex-2.5.33/m4-1.4.10/flex-2.5.33'
make: *** [install] Error 2 Posted by: nishath on May 19, 2012

Error: Unable to locate specified base class 'mx.core.Application' for component class

This is the error I receive when I execute mxmlc mytest.mxml

I am running ubuntu 12.04 Posted by: Shekhar on Jun 25, 2012

tar (child): flex-2.5.33.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now Posted by: Sambartika Guha on Aug 23, 2013

@sambartika: looks like you downloaded the tar.gz file in a different directory ... please try to find out where you actually downloaded the file Posted by: shahryar on Sep 20, 2013

checking for help2man... help2man
checking for gm4... no
checking for gnum4... no
checking for m4... no
checking for GNU m4... no
configure: error: GNU M4 1.4 is required
I have install M4 but still showing this error
and even I extracted it Posted by: Parth on Sep 23, 2013

I have downloaded n extracted m4 file in user n my flex file in user only .
do we have to run the path file before cd flex or afterwards .
Thanks in advance Posted by: Parth on Sep 23, 2013

@Parth: not sure what you mean ... you need to install m4 and specify the path to that "installed" m4 location in configure script for flex

here is how to install m4: Posted by: shahryar on Oct 09, 2013

I have installed the flex as u have told..But again it says that the lex command is not recognized.. Posted by: joselin on Jan 21, 2014

@joselin: Are you trying something from within /usr/local/flex?

Can you paste outputs please? Posted by: shahryar on Apr 08, 2014

I get this error:
tar (child): flex-2.5.33.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now Posted by: lango on Nov 30, 2014

@lango: You may be trying to open the wrong file or tyring to open it from a wrong directory (where it does not exist) Posted by: geeksww on Jan 01, 2015

I had to install m4 and flex: apt-get install flex on 14.04 LTS Posted by: boo on Apr 17, 2015

I get this error:
tar (child): flex-2.6.0.tar.bz2: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now Posted by: ranjana dahal on Feb 06, 2018

leave a comment on tutorial leave a comment