Installing a CUPS printer via the command line under Ubuntu


I try to script everything I can on Linux.  When I install something on my desktop, I like to be able to run a script to install the same thing on my laptop.  When a computer breaks, I like to be able to run all the scripts to have it set up again just the way that it was. Here is how I scripted the installation of my printer.

I own a Brother HL4570CDW color laser printer.  It is a network printer that uses standard postscript drivers.  I have it installed on my home network at a static IP address: 192.168.1.9.    Having worked for the laser printer company GCC Printers, I know that to install this type of business printer, you generally only need a “PostScript Printer Description” (PPD) file.  The PPD file is little more than a configuration text file that lists all the options that the printer supports.

Ubuntu comes with PPD files for a large number of printers preinstalled.  My particular printer is not in the list of printers in Ubuntu’s “add printer wizard”.  Fortunately, Brother supports Linux (and Ubuntu) very well.  I could download installable packages that provide this PPD and put it into the lists available to the make it available to both the LPD and CUPS printing systems.   From Brother’s Linux driver download page, I downloaded both the LPD and the CUPS drivers in deb format.   The CUPS driver depends on the LPD driver, so to get printing working for most programs in Ubuntu, you need both drivers.  These drivers want to use /var/spool/lpd which is not a directory that Ubuntu has by default. To get the drivers to install properly, I had to create this directory for them.   Then I could install them:
sudo mkdir -p /var/spool/lpd/
sudo dpkg --install hl4570cdw*.deb

At this point, I could install the printer using the GUI tools available under Ubuntu.

Next I needed to gather information for the command line tools. A list of all printer makes and models that can be installed is available with the command lpinfo -m. Using this command, I found that the model name that the command line tools expect for my printer is “HL-4570CDW series CUPS” and its PPD file is “brhl4570cdw.ppd”. I found the PPD file installed at its full path by the brother drivers: /usr/share/ppd/brhl4570cdw.ppd. Once the printer is installed, you cane use the command lpoptions -l to figure out the various options available for your particular printer. I set some of these options for my printer.

Now I could construct a command line to install my printer. Printers can be installed with lpadmin. Here are the command line options that I settled on:

  • -p 'Brother-HL-4570CDW' – The name of the installed printer
  • -v 'socket://192.168.1.9' – The network address of the printer
  • -m 'HL-4570CDW series CUPS‘ – The make and model of the printer from lpinfo -m
  • -P '/usr/share/ppd/brhl4570cdw.ppd' – Path to the ppd file
  • -L 'Basement Utility Room' – Location of the printer
  • -o 'Two-Sided Printing=DuplexNoTumble' – Option to enable long edge duplex
  • -o 'Color=Mono' – Disable color printing by default
  • -o 'Enhance Black Printing=ON' – Enhance black
  • -o 'Skip Blank Page=ON' – Skip blank pages
  • -E – Enable the printer

I wanted to make my printer the default printer on my system, so I use the command lpadmin -d 'Brother-HL-4570CDW'

At this point, the printer is installed from the command line, however, I discovered that lpoptions -l knows about the printer options (like duplex) that I chose, but CUPS does not. Neither the printer properties in the GUI (system-config-printer) nor the local CUPS administration webpage (http://localhost:631/printers/) showed that duplex was enabled for the printer. After some research, I found that CUPS stores its own options outside of LPD. It creates a copy of the PPD, and rewrites that PPD every time you change the printer options. It ignores the LPD options set with lpadmin or lpoptions. The CUPS copy of the PPD is in /etc/cups/ppd/Brother-HL-4570CDW.ppd. I used some sed commands to modify that file in place the same way that the CUPS GUI does it.

  • s/DefaultBRDuplex.*/DefaultBRDuplex: DuplexNoTumble/g;
  • s/DefaultBRMonoColor.*/DefaultBRMonoColor: mono/g;
  • s/DefaultBREnhanceBlkPrt.*/DefaultBREnhanceBlkPrt: ON/g;
  • s/DefaultBRSkipBlank.*/DefaultBRSkipBlank: ON/g;

So here is the final full script to install the printer from the command line:

# Check to see if the drivers are installed
if [ ! -e /usr/share/ppd/brhl4570cdw.ppd ]
then
    sudo mkdir -p /var/spool/lpd/
    sudo dpkg --install hl4570cdw*.deb
fi

# Check to see if the printer is already installed
if [ `lpstat -p 2>&1 | grep -E 'HL.?4570.?CDW' -c || true` = "0" ]
then
    # Make sure the printer is available before trying to install it (check the printer web page)
    if [ `curl -s http://192.168.1.9/main/main.html | grep -c 'Brother HL-4570CDW' || true` = "0" ]
    then
        echo "Printer cannot be found at http://192.168.1.9/main/main.html"
        exit 1
    fi
    echo "Installing Brother-HL-4570CDW"
    lpadmin \
        -p 'Brother-HL-4570CDW' \
        -v 'socket://192.168.1.9' \
        -m 'HL-4570CDW series CUPS' \
        -P '/usr/share/ppd/brhl4570cdw.ppd' \
        -L 'Basement Utility Room' \
        -o 'Two-Sided Printing=DuplexNoTumble' \
        -o 'Color=Mono' \
        -o 'Enhance Black Printing=ON' \
        -o 'Skip Blank Page=ON' \
        -E
    # Set it as the default printer
    lpadmin -d 'Brother-HL-4570CDW'
    echo "Setting up CUPS default print options"
    sudo sed -ir 's/DefaultBRDuplex.*/DefaultBRDuplex: DuplexNoTumble/g;  
                  s/DefaultBRMonoColor.*/DefaultBRMonoColor: mono/g;
                  s/DefaultBREnhanceBlkPrt.*/DefaultBREnhanceBlkPrt: ON/g;
                  s/DefaultBRSkipBlank.*/DefaultBRSkipBlank: ON/g;' /etc/cups/ppd/Brother-HL-4570CDW.ppd
fi

 


Leave a comment

Your email address will not be published. Required fields are marked *

3 thoughts on “Installing a CUPS printer via the command line under Ubuntu

  • Alex

    Hey – thanks for the lpadmin overview and the sample script. I have a job to install hundreds printers and there is no way I’m using the GUI.

  • Michael Rynish

    I am a relative novice to UNIX and am totally unfamiliar with Ubuntu. I too have an old GCC printer which has been a workhorse since I purchased it in 1995. I have just migrated to Mac OX 10.9.1 and I have been having difficulty printing to to my GCC Elite 1200/1200n . In System Preferences I can ‘add’ the printer using the Printer Scanner feature with the printer on my network at 10.0.1.3. When I send a print to this printer from Word or other programs the file is sent to the printer, but it is like the printer doesn’t know what to do and it ‘times out’ and never prints. Can you help me out with instructions for using CUPS to create a PPD file for my GCC printer?

    • Stephen Post author

      I helped develop that printer. The PPD files for that printer are still available from the GCC Website: http://gccprinters.com/support/doc/elite12.html I downloaded the windows driver http://ftp.gcctech.com/pub/postscript-pcl/elite-12ppm/windows/elite12.exe and extracted it using the command: “wine elite12.exe” There are a number of PPD files in that download the one for the “GCC Elite 1200/1200n” is GCE12UT1.PPD The Mac downloads should also have that file in them and you might have an easier time getting to them on OS X by downloading the drivers for the Mac.